<!--
描述：试卷管理-自动组卷
开发人：fwb
开发日期：2018年05月22日
-->

<template lang="pug">
    div.kalix-article
        keep-alive
            el-row.frame-row(:gutter="10")
                el-col.s-flex.flex-column.frame-col(v-bind:span="20")
                    div.s-flex_item.frame-item
                        keep-alive
                            kalix-table(bizKey="enrolmentPaper" title='试卷列表' v-bind:targetURL="paperTargetURL"
                            v-bind:bizDialog="paperBizDialog" v-bind:isFixedColumn="isFixedColumn"
                            v-bind:toolbarBtnList="paperToolbarBtnList"  v-bind:customToolBar="paperCustomToolBar"
                            v-bind:btnList="paperBtnList" v-bind:customTableTool="paperCustomTableTool"
                            v-on:onTableRowClick="onTableRowClick" ref="paperTable" v-bind:jsonStr="jsonStr" v-bind:dictDefine="dictDefine")
                                template(slot="tableColumnSlot")
                                    el-table-column(prop="year" label="年份" align="center" )
                                    el-table-column(prop="termName" label="学期" align="center" )
                                    el-table-column(prop="title" label="试卷名称" align="center" )
                                    el-table-column(label="考试科目" align="center" )
                                        template(slot-scope="scope")
                                            template(v-if="scope.row.tempName === '1'") {{scope.row.kskmName}}
                                            template(v-else-if="scope.row.tempName === '2'") {{scope.row.kskmName2}}
                                            template(v-else-if="scope.row.tempName === '3'") {{scope.row.kskmName3}}
                                    el-table-column(prop="totalMark" label="总分" align="center" )
                                    el-table-column(prop="copies" label="自动成卷份数" align="center" )
                    div.s-flex_item.frame-item
                        keep-alive
                            kalix-table(bizKey="enrolmentRule" title='参数列表' v-bind:targetURL="ruleTargetURL"
                            v-bind:bizDialog="ruleBizDialog" v-bind:isFixedColumn="isFixedColumn"
                            v-bind:toolbarBtnList="ruleToolbarBtnList" v-bind:customToolBar="ruleCustomToolBar"
                            v-bind:btnList="ruleBtnList" v-bind:customTableTool="ruleCustomTableTool" v-bind:dictDefine="dictDefine"
                            ref="ruleTable")
                                template(slot="tableColumnSlot")
                                    el-table-column(prop="titleNumName" label="试题顺序" align="center" )
                                    el-table-column(prop="quesTypeName" label="试题题型" align="center" )
                                    el-table-column(prop="quesScore" label="每题分数" align="center" )
                                    el-table-column(prop="quesTotalscore" label="试题总分" align="center" )
                                    el-table-column(prop="quesDesc" label="题型描述" align="center" )
</template>

<script type="text/ecmascript-6">
    import PaperFormModel from './paperModel'
    import RuleFormModel from './ruleModel'
    import {AutoPaperRuleURL, AutoPaperURL} from '../config.toml'
    import {paperBtnList, ruleBtnList} from './config.js'

    export default {
        name: 'kalix-enrolment-autopaper',
        data() {
            return {
                paperFormModel: Object.assign({}, PaperFormModel),
                ruleFormModel: Object.assign({}, RuleFormModel),
                dictDefine: [{
                    cacheKey: 'ENROLMENT-DICT-KEY',
                    type: '题型顺序',
                    targetField: 'titleNumName',
                    sourceField: 'titleNum'
                }, {
                    cacheKey: 'ENROLMENT-DICT-KEY',
                    type: '题型',
                    targetField: 'quesTypeName',
                    sourceField: 'quesType'
                }, {
                    cacheKey: 'ENROLMENT-DICT-KEY',
                    type: '考试科目',
                    targetField: 'kskmName',
                    sourceField: 'kskm'
                }, {
                    cacheKey: 'ENROLMENT-DICT-KEY',
                    type: '面试题类型',
                    targetField: 'kskmName3',
                    sourceField: 'kskm'
                }, {
                    cacheKey: 'ENROLMENT-DICT-KEY',
                    type: '主观题类型',
                    targetField: 'kskmName2',
                    sourceField: 'kskm'
                }, {
                    cacheKey: 'ENROLMENT-DICT-KEY',
                    type: '学期',
                    targetField: 'termName',
                    sourceField: 'term'
                }],
                jsonStr: '{"delFlag": "0"}',
                toolbarBtnList: [
                    {id: 'add', isShow: false},
                    {id: 'refresh', isShow: true, icon: 'icon-refresh', title: '刷新'}
                ],
                // 试卷规则和试题规则列表
                isFixedColumn: true,
                // 试卷规则列表
                paperTargetURL: AutoPaperURL,
                paperToolbarBtnList: [
                    {id: 'add', isShow: false},
                    {id: 'newAdd', title: '添加', isShow: true, icon: 'icon-add'}
                ],
                paperBtnList: paperBtnList,
                paperBizDialog: [
                    {id: 'newEdit', dialog: 'PaperModuleAdd'},
                    {id: 'newAdd', dialog: 'PaperModuleAdd'}
                ],
                // 试题规则列表
                ruleTargetURL: AutoPaperURL + '/0/rules',
                ruleToolbarBtnList: [
                    {id: 'add', isShow: false},
                    {id: 'newAdd', title: '添加', isShow: true, icon: 'icon-add'}
                ],
                ruleBtnList: ruleBtnList,
                ruleBizDialog: [
                    {id: 'newEdit', dialog: 'RuleAdd'},
                    {id: 'newAdd', dialog: 'RuleAdd'}
                ]
            }
        },
        components: {},
        created() {
        },
        mounted() {
        },
        computed: {},
        watch: {},
        methods: {
            // 试卷规则列表自定义工具栏
            paperCustomToolBar(btnId, table) {
                switch (btnId) {
                    case 'newAdd': {
                        this.onPaperNewAdd(table)
                        break
                    }
                }
            },
            // 添加试卷规则
            onPaperNewAdd(table) {
                let dig =
                    table.bizDialog.filter((item) => {
                        return item.id === 'newAdd'
                    })
                table.whichBizDialog = dig[0].dialog
                setTimeout(() => {
                    this.paperFormModel = Object.assign({}, PaperFormModel)
                    table.$refs.kalixDialog.$refs.kalixBizDialog.open('添加', false, this.paperFormModel)
                }, 20)
            },
            // 试卷规则列表自定义操作栏
            paperCustomTableTool(row, btnId, that) {
                switch (btnId) {
                    case 'newEdit' : {
                        this.onPaperNewEdit(row, that)
                        break
                    }
                    case 'newDelete' : {
                        this.onPaperNewDelete(row, that)
                        break
                    }
                }
            },
            // 编辑试卷规则
            onPaperNewEdit(row, table) {
                let dig =
                    table.bizDialog.filter((item) => {
                        return item.id === 'newEdit'
                    })
                table.whichBizDialog = dig[0].dialog
                setTimeout(() => {
                    row.targetURL = this.paperTargetURL
                    row.isEdit = true
                    row.currentYears = []
                    if (row.quesRange) {
                        if (row.quesRange.indexOf(",") > 0) {
                            let str = row.quesRange.split(",")
                            for (let i = 0; i < str.length; i++) {
                                row.currentYears.push(str[i] * 1)
                            }
                        }
                    }
                    table.$refs.kalixDialog.$refs.kalixBizDialog.open('编辑', true, row)
                }, 20)
            },
            // 删除试卷规则
            onPaperNewDelete(row, table) {
                this.$confirm('确定要删除吗?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    return this.axios.request({
                        method: 'DELETE',
                        url: AutoPaperURL + '/delete/logicDelete',
                        params: {},
                        data: {
                            id: row.id
                        }
                    })
                }).then(response => {
                    table.getData()
                    this.$KalixMessage.success(response.data.msg)
                }).catch(() => {
                })
            },
            onPaperdoPaper(row, table) {
                this.axios.request({
                    headers: {},
                    method: 'get',
                    url: ExportExlURL,
                    params: {
                        jsonStr: queryParams,
                    }
                }).then(response => {
                    if (response.data) {
                        this.download(response.data)
                        this.$KalixMessage.success('导出成功！')
                    } else {
                        this.$KalixMessage.error('导出失败！')
                    }
                }).catch((e) => {
                    this.$KalixMessage.error('导出失败！原因：' + e.message)
                })
            },
            onTableRowClick(row, event, column) {
                if (this.paperRow) {
                    if (this.paperRow.id !== row.id) {
                        // 执行查询试卷组卷试题规则数据
                        this.clearTableData(2)
                        this.paperRow = row
                        this.getRuleTableData(this.paperRow.id)
                    }
                } else {
                    // 执行查询试卷组卷试题规则数据
                    this.clearTableData(2)
                    this.paperRow = row
                    this.getRuleTableData(this.paperRow.id)
                }
            },
            // 查询试题规则列表数据
            getRuleTableData(paperId) {
                if (paperId) {
                    this.ruleTargetURL = AutoPaperURL + '/' + paperId + '/rules'
                } else {
                    this.ruleTargetURL = AutoPaperRuleURL + '/0'
                }
            },
            // 清除列表数据0全部，1试卷规则，2试题规则
            clearTableData(num) {
                switch (num) {
                    // 清除所有列表数据
                    case 0 :
                        this.$refs.paperTable.clearData()
                        this.$refs.ruleTable.clearData()
                        break
                    case 1 :
                        // 清除试卷规则列表数据
                        this.$refs.paperTable.clearData()
                        break
                    case 2 :
                        // 清除试题规则列表数据
                        this.$refs.ruleTable.clearData()
                        break
                    default :
                        this.$refs.moduleTable.clearData()
                        this.$refs.menuTable.clearData()
                        break
                }
            },
            // 菜单列表自定义工具栏
            ruleCustomToolBar(btnId, table) {
                switch (btnId) {
                    case 'newAdd': {
                        this.onRuleNewAdd(table)
                        break
                    }
                }
            },
            // 添加试卷参数
            onRuleNewAdd(table) {
                let dig =
                    table.bizDialog.filter((item) => {
                        return item.id === 'newAdd'
                    })
                table.whichBizDialog = dig[0].dialog
                setTimeout(() => {
                    this.ruleFormModel = Object.assign({}, RuleFormModel)
                    this.ruleFormModel.targetURL = this.ruleTargetURL
                    this.ruleFormModel.paperId = this.paperRow.id
                    this.ruleFormModel.isEdit = false
                    this.ruleFormModel.currentYears = []
                    table.$refs.kalixDialog.$refs.kalixBizDialog.open('添加', false, this.ruleFormModel)
                }, 20)
            },
            // 菜单列表自定义操作栏
            ruleCustomTableTool(row, btnId, that) {
                switch (btnId) {
                    case 'newEdit' : {
                        this.onRuleNewEdit(row, that)
                        break
                    }
                    case 'newDelete' : {
                        this.onRuleNewDelete(row, that)
                        break
                    }
                }
            },
            // 编辑菜单
            onRuleNewEdit(row, table) {
                let dig =
                    table.bizDialog.filter((item) => {
                        return item.id === 'newEdit'
                    })
                table.whichBizDialog = dig[0].dialog
                setTimeout(() => {
                    row.rows = []
                    row.targetURL = this.ruleTargetURL
                    row.isEdit = true
                    row.currentYears = []
                    if (row.quesRange) {
                        if (row.quesRange.indexOf(",") > 0) {
                            let str = row.quesRange.split(",")
                            for (let i = 0; i < str.length; i++) {
                                row.currentYears.push(str[i] * 1)
                            }
                        }
                    }
                    if (row.typeCount) {
                        if (row.typeCount.indexOf(";") > 0) {
                            let str = row.typeCount.split(";")
                            for (let i = 0; i < str.length - 1; i++) {
                                let keyValue = str[i].split(",");
                                row.rows.push({
                                    subType: keyValue[0],
                                    typeCount: keyValue[1]
                                })
                            }

                        }
                    }
                    table.$refs.kalixDialog.$refs.kalixBizDialog.open('编辑', true, row)
                }, 20)
            },
            // 删除参数
            onRuleNewDelete(row, table) {
                this.$confirm('确定要删除吗?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.axios.request({
                        method: 'DELETE',
                        url: AutoPaperRuleURL + '/delete/logicDelete',
                        params: {},
                        data: {
                            id: row.id
                        }
                    }).then(response => {
                        table.getData()
                        this.$KalixMessage.success(response.data.msg)
                    })
                }).catch(() => {
                })
            }
        }
    }
</script>

<style lang='stylus' type='text/stylus'>
    .frame-row .frame-col
        width 100%
</style>
